package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.MakeInfo;
import cn.shlbysj.demo.entity.MakeInfoExample;
import cn.shlbysj.demo.mapper.MakeInfoMapper;
import cn.shlbysj.demo.service.IMakeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("makeService")
@Transactional
public class IMakeServiceImpl implements IMakeService {

    @Autowired
    private MakeInfoMapper makeInfoDao;

    @Override
    public PageResult findPage(String name, Integer state, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        MakeInfoExample example=new MakeInfoExample();
        example.setOrderByClause("`create_time` desc");
        MakeInfoExample.Criteria criteria = example.createCriteria();
        if(name!=null && !"".equals(name)){
            criteria.andNameLike(name);
        }
        if(state!=null && !"".equals(state.toString())){
            criteria.andStateEqualTo(state);
        }
        Page<MakeInfo> pageList = (Page<MakeInfo>) makeInfoDao.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public Result insert(MakeInfo makeInfo) {
        makeInfo.setmId(UuidUtil.getUUID());
        makeInfo.setState(ConstansUtil.MAKE_NO_DEAL);
        makeInfo.setCreateTime(new Date());
        int ret=makeInfoDao.insert(makeInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public List<MakeInfo> getListTop() {
        PageResult page=this.findPage(null,null,1,5);
        return page.getRows();
    }

    @Override
    public Result update(MakeInfo makeInfo) {
        int ret=makeInfoDao.updateByPrimaryKeySelective(makeInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
}
